/**
 * 训练经历相关API - 使用统一的运动员API
 */
import type { AthleteTrainExp } from '@/api/athlete/types'
import { getTrainExpListApi, createTrainExpApi, updateTrainExpApi, deleteTrainExpApi } from '@/api/athlete'

/**
 * 查询训练经历列表
 */
export const getTrainingHistoryApi = (infoId: string | number) => {
	return getTrainExpListApi(infoId)
}

/**
 * 新增训练经历
 */
export const addTrainingHistoryApi = (data: Partial<AthleteTrainExp>) => {
	// 处理级联选择器数据
	const submitData = { ...data }
	if (data.trainOrgCascade && Array.isArray(data.trainOrgCascade) && data.trainOrgCascade.length > 0) {
		// 级联选择器的最后一级就是 trainOrgId
		submitData.trainOrgId = data.trainOrgCascade[data.trainOrgCascade.length - 1]
		delete submitData.trainOrgCascade
	}

	return createTrainExpApi(submitData)
}

/**
 * 更新训练经历
 */
export const updateTrainingHistoryApi = (id: string | number, data: Partial<AthleteTrainExp>) => {
	// 处理级联选择器数据
	const submitData: Partial<AthleteTrainExp> = {
		...data,
		id: typeof id === 'string' ? parseInt(id) : id
	}
	if (data.trainOrgCascade && Array.isArray(data.trainOrgCascade) && data.trainOrgCascade.length > 0) {
		// 级联选择器的最后一级就是 trainOrgId
		submitData.trainOrgId = data.trainOrgCascade[data.trainOrgCascade.length - 1]
		delete submitData.trainOrgCascade
	}

	return updateTrainExpApi(submitData)
}

/**
 * 删除训练经历
 */
export const deleteTrainingHistoryApi = (id: string | number) => {
	return deleteTrainExpApi(id)
}
